package com.ellwood.IPGuard.common.core.basic;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ellwood.IPGuard.common.core.model.pojo.EllPage;

/**
 * @ProjectName: IPGuard-boot
 * @PackageName: com.ellwood.IPGuard.common.core.basic
 * @ClassName: ServiceFactoryImpl
 * @Description: // 基础服务类，所有Service都要继承
 * @Author: Ellwood
 * @CreateDate: 2024/9/25 - 下午3:55
 * @Version: V1.0.0
 */
// 因使用DDD架构，所以这里的service不在继承mybatisPlus的IService接口，只做普通基类
//public class ServiceFactoryImpl<M extends BaseMapper<T>,T> extends ServiceImpl<M,T> implements ServiceFactory<T> {
public class ServiceFactoryImpl<T> implements ServiceFactory<T> {
        
        /**
         * 获取分页对象
         *
         * @param pageBase 分页参数
         * @return 分页对象
         */
        protected IPage<T> getPage(EllPage pageBase) {
                Page<T> page = new Page<>(pageBase.getPage(), pageBase.getLimit());
                
                // 排序
                if (StringUtils.isNotBlank(pageBase.getOrder())) {
                        if (pageBase.isAsc()) {
                                return page.addOrder(OrderItem.asc(pageBase.getOrder()));
                        } else {
                                return page.addOrder(OrderItem.desc(pageBase.getOrder()));
                        }
                }
                
                return page;
        }
        
}
